from django.contrib.auth.models import AbstractUser
from django.db import models

from utils import constants


class User(AbstractUser):
    """自定义用户模型类"""
    GENDER_CHOICES = {
        (0, "MAN"),
        (1, "WOMAN")
    }
    gender = models.SmallIntegerField(choices=GENDER_CHOICES, default=1, verbose_name="性别")
    mobile = models.CharField(max_length=11, unique=True, verbose_name='手机号')  # 手机号
    avatar_url = models.CharField(max_length=250, default='')  # 用户头像路径
    signature = models.CharField(max_length=500)  # 用户签名

    class Meta:
        db_table = 'info_user'
        verbose_name = '用户'
        verbose_name_plural = verbose_name

    def to_dict(self):
        resp_dict = {
            "id": self.id,
            "username": self.username,
            "avatar_url": constants.QINIU_DOMIN_PREFIX + self.avatar_url if self.avatar_url else "",
            "mobile": self.mobile,
            "gender": self.gender if self.gender else "MAN",
            "signature": self.signature if self.signature else "",
            "followers_count": self.followers.count(),
            "news_count": self.news_list.count()
        }
        return resp_dict
